class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            pwd: '',
            phone: '',
            rpwd: '',
            district: [
                ''
            ],
        };
    }









    componentDidMount() {
        if (Constant.SWITCH_VCONSOLE) {
            new VConsole();
        }

        // let openid = localStorage.getItem('openid')
        // getuser(openid, res => {
        //     if (res.issuccess == 1) {
        //         let user = res.user
        //         this.setState({ user, allprice:835, amount:  835 })
        //     }
        // })

        // let sizehtml = [],cphtml=[], { sizelist } = this.state;

        // sizehtml.push(<option value={-1}>请选择</option>)
        // sizelist.forEach((x, i) => {
        //     sizehtml.push(
        //         <option key={i} value={x}>{x} </option>
        //     )
        // })
        // cphtml.push(<option value={1}>大包(￥535元)</option>)
        // cphtml.push(<option value={0}>小包(￥310元)</option>)
        // cphtml.push(<option value={-1}>不购买</option>)

        // this.setState({ sizehtml,openid,cphtml })

        //  console.log(liststr?'1':'2','liststr');
        // let list = JSON.parse(localStorage.getItem('orderlist')) || []
        // console.log(list, 'list');

        // this.setState({ list }, () => {
        //     this.getamount()
        // })
        // this.getdata()

    }

    pay(size) {
        let { openid, amount } = this.state;
        praypay(openid, Number(amount) * 100, size, res => {
            console.log(res, 'resres');
            let data = JSON.parse(res.ret)
            this.requestpay(data)
        })

    }

    requestpay(data) {
        function onBridgeReady() {

            let sign = md5(`appId=wxa29d77bb2e0c40bb&nonceStr=${data.nonce_str}&package=${data.package}&signType=MD5&timeStamp=${data.timeStamp}&key=Y3A7lWq2Xsd7ScCTSp8QUqLiWELxHCl7`).toUpperCase()
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', {
                "appId": "wxa29d77bb2e0c40bb",     //公众号ID，由商户传入     
                "timeStamp": data.timeStamp,         //时间戳，自1970年以来的秒数     
                "nonceStr": data.nonce_str, //随机串     
                "package": data.package,
                "signType": "MD5",         //微信签名方式：     
                "paySign": sign //微信签名 
            },

                function (res) {
                    if (res.err_msg == "get_brand_wcpay_request:ok") {

                        // 使用以上方式判断前端返回,微信团队郑重提示：
                        //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                    }
                });
        }
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
            }
        } else {
            onBridgeReady();
        }
    }



    inputbz(e) {
        this.setState({ desc: e.target.value })
    }

    clear() {
        var b = confirm('确定要清空吗')
        if (b) {
            localStorage.removeItem('orderlist')
            this.setState({ list: [], desc: '', amount: 0 })
        }
    }

    gopay() {
        let { sizeid, openid } = this.state;
        if (sizeid == '' || sizeid == '请选择') {
            alert('请先选择尺寸')
        } else {

            canbuy(openid, res => {
                if (res.data == true) {
                    this.pay(sizeid)
                } else {
                    alert('请勿重复购买')
                }

            })
            // 
        }

    }
    toxieyi(e) {
        window.location.href = './xieyi.html?t=' + e
    }

    selectchange(e) {
        let { value } = e.target
        this.setState({ sizeid: value })
    }
    cbchange(e) {
        //console.log('cbchange', e.target.checked);
        this.setState({ isagree: e.target.checked })
    }
    inputchange(type, e) {
        console.log('inputchange', { type, e });
        //  let { value } = e.target
        this.setState({ [type]: e.target.value })
    }

    selectchange1(e) {
        let { value } = e.target, { amount, allprice } = this.state;
        if (value == 0) {
            amount = 610
        } if (value == -1) {
            amount = 300
        } if (value == 1) {
            amount = 835
        }
        this.setState({ cpid: value, amount })
    }
    submit() {
        if (!this.state.isagree) {
            alert('请先勾选同意协议')
        } else {
            let { phone, pwd, rpwd } = this.state, err = ''
            if (!checkPhone(phone)) { err = '手机号输入有误' }
            if (pwd == '') { err = '请输入密码' }
            else if (rpwd == '') {
                err = '请再次输入密码'
            }
            else if (rpwd != pwd) {
                err = '两次输入密码不一致'
            }
            if (err != '') {
                alert(err)
            } else {
                console.log({ phone, pwd, rpwd }, 'sumbit');
                注册({phone,pwd,userid:1},res=>{
                    if(res.issuccess==1){
                        alert('注册成功')
                        window.location.href='./login.html'
                    }else{
                        alert(res.msg)
                    }
                })

            }

        }
    }


    render() {
        let { phone, pwd, rpwd, isagree, sizeid, cphtml, cpid } = this.state;
        //console.log(sizehtml, 'sizehtml');
        return (
            <div className="container">

                <div className="title">注册</div>
                <div class="app">
                    <div className="d1">
                        <p>账户</p>
                        <input type="text" placeholder="请输入手机号" maxLength={11} value={phone} onChange={this.inputchange.bind(this, 'phone')} />
                    </div>
                    <div className="d1">
                        <p>密码</p>
                        <input type="text" placeholder="请输入密码" value={pwd} onChange={this.inputchange.bind(this, 'pwd')} />
                    </div>
                    <div className="d1">
                        <p>确认密码</p>
                        <input type="text" placeholder="请再次输入密码" value={rpwd} onChange={this.inputchange.bind(this, 'rpwd')} />
                    </div>

                </div>
                <div className="cb">
                    <input type="checkbox" onChange={this.cbchange.bind(this)} checked={isagree} />
                    <div>
                        <p>我已阅读并同意</p>
                        <p onClick={this.toxieyi.bind(this, 'yhxy')} className="blue">《用户协议》</p>
                        以及
                        <p onClick={this.toxieyi.bind(this, 'yszc')} className="blue">《隐私保护政策》</p>
                    </div>
                </div>
                <div onClick={this.submit.bind(this)} className="submit">提交 </div>
            </div>
        )
    }
}

ReactDOM.render(
    <div>
        {/* <TopLine /> */}
        <div id='body'>
            {/* <Tabbar index='1' /> */}
            <Main />
        </div>
    </div>
    // <Barrage />
    , $('.container')[0]
)